#!groovy

def call(String jenkinsLabel,String projectName) {
    echo projectName
    echo jenkinsLabel
    node("${jenkinsLabel}"){
        def KUTUI_GROUP="https://push.xuthus.cc/group/"
        def DEPLOY_VERSION="SNAPSHOT-1.${BUILD_NUMBER}"

        try {
            def PROJECT_NAME = projectName.toLowerCase()
            def NODE_HOSTNAME='master'

            def HTTP_PORT='4000:80'
            if(env.BRANCH_NAME == "master"){
                HTTP_PORT='8000:80'
                DEPLOY_VERSION="RELEASE-1.${BUILD_NUMBER}"
            }
            def GIT_URL = 'https://gitee.com/crowdmall/test7.git'
            ////////////////////////////以下是模板
            def ARTI_IPPORT = '10.21.1.105:8083'
            //git
            def GIT_CREDENTIALS_ID = 'crowdmall'
            withCredentials([usernamePassword(credentialsId: '1c6eb9ba-7a49-4611-89b3-35feb3c80725', passwordVariable: 'password', usernameVariable: 'username')]) {
                sh "docker login -u ${username} -p ${password} ${ARTI_IPPORT} "
            }
            docker.image(ARTI_IPPORT+'/docker-local/yiyun/npm:latest').inside(" -v /var/run/docker.sock:/var/run/docker.sock -v /root/.npm:/root/.npm -v /usr/bin/docker:/usr/bin/docker --user root ") {
                stage('Prepare') {
                    echo "${env.BRANCH_NAME}"
                    echo "环境准备"
                }

                stage('SCM') {
                    git url: GIT_URL,
                            branch: env.BRANCH_NAME,
                            changelog: true,
                            credentialsId: GIT_CREDENTIALS_ID
                }

                stage('add jiraResult') {
                    if (env.BRANCH_NAME == "master") {
                        echo "add jiraResult"
                        //def requirements = getRequirementsIds();
                        //echo "requirements : ${requirements}"

                        //def revisionIds = getRevisionIds();
                        //echo "revisionIds : ${revisionIds}"
                        //rtMaven.deployer.addProperty("project.issues", requirements).addProperty("project.revisionIds", revisionIds)
                    }
                }
                //进行测试
                stage('basic test') {
                    if (env.BRANCH_NAME == "master") {
                        echo "add test step"
                    }
                }
                stage('xray scan') {
                    if (env.BRANCH_NAME == "master") {
                        //def xrayConfig = [
                        //        'buildName'  : buildInfo.name,
                        //        'buildNumber': buildInfo.number,
                        //        'failBuild'  : false
                        //]
                        // def xrayResults = artiServer.xrayScan xrayConfig
                        // echo xrayResults as String
                    }
                }

                stage('npm build'){
                    sh '''
                            cd web/admin/
                            rm -rf package-lock.json
                            npm config set registry "'''+ARTI_IPPORT+'''/artifactory/npm-virtual/"
                            npm config set _auth "bHpxOkx6cXpodTEyNDFqaWU="
                            npm install echarts@^4.1.0 --ignore-scripts
                            npm install  --verbose
                            npm run build:test --verbose
                        '''
                }

                stage('build && push'){
                    withCredentials([usernamePassword(credentialsId: '1c6eb9ba-7a49-4611-89b3-35feb3c80725', passwordVariable: 'password', usernameVariable: 'username')]) {
                        sh '''
                                cd web/admin/
                                docker login -u '''+"${username}"+''' -p '''+"${password}"+''' 10.21.1.105:8083
                                docker build -f ./Dockerfile -t 10.21.1.105:8083/docker-local/'''+"${projectName}"+''':'''+"${DEPLOY_VERSION}"+''' .
                                docker push '''+ARTI_IPPORT+'''/docker-local/'''+"${projectName}"+''':'''+"${DEPLOY_VERSION}"+'''
                            '''
                    }
                }

                stage('deploy'){
                    sh '''
                            export HTTP_PORT='''+HTTP_PORT+'''
                            export NODE_HOSTNAME=''' + "${NODE_HOSTNAME}" + '''
                            export SERVICE_IMAGE='''+ARTI_IPPORT+'''/docker-local/'''+projectName+''':'''+DEPLOY_VERSION+'''
                            docker stack deploy -c jenkins/admin-docker-compose.yml --with-registry-auth '''+projectName+'''-'''+"${env.BRANCH_NAME}"+'''
                        '''
                }
            }
            withCredentials([string(credentialsId: 'kutui-token', variable: 'KUTUI_KEY')]) {
                echo "完成构建，发qq"
                sh "curl ${KUTUI_GROUP}/$KUTUI_KEY?c=构建状态:SUCCESS:$projectName:${DEPLOY_VERSION}-${env.BRANCH_NAME}"
            }
        }catch(exception1){
            withCredentials([string(credentialsId: 'kutui-token', variable: 'KUTUI_KEY')]) {
                echo "构建异常，发qq"
                sh "curl ${KUTUI_GROUP}/$KUTUI_KEY?c=构建状态:FAILD:$projectName:${DEPLOY_VERSION}-${env.BRANCH_NAME}"
            }
            throw exception1
        }
    }
}